/*
 * ReminderDialog.java
 *
 * Created on Jan 13, 2009, 9:41:02 PM
 */
package com.jvege.view;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.KeyStroke;
import com.jvege.entity.Reminder;
import com.jvege.model.ReminderManager;

/**
 *
 * @author Dickson
 */
public class ReminderDialog extends javax.swing.JDialog implements WindowListener {

    private Reminder reminder;

    /** Creates new form ReminderDialog */
    public ReminderDialog(java.awt.Frame parent, boolean modal, Reminder reminder) {
        super(parent, modal);
        initComponents();
        postInitComponent();
        this.reminder = reminder;
        // add <html> to enable label to wrap text
        lblReminder.setText("<html>" + reminder.getTask() + "</html>");
    }

    // add the key binding for 2 button, in other for Enter key to get function
    private void postInitComponent() {
        btnSnooze.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "performSnooze");
        btnSnooze.getActionMap().put("performSnooze", snoozeAction);
        btnDismiss.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "performDismiss");
        btnDismiss.getActionMap().put("performDismiss", dismissAction);
        this.addWindowListener(this);
        //pnReminder.setSize(new Dimension(100,100));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnReminder = new GradientPanel("dialog.box.panel.start", "dialog.box.panel.end", "V");
        lblReminder = new javax.swing.JLabel();
        pnButton = new javax.swing.JPanel();
        btnDismiss = new javax.swing.JButton();
        btnSnooze = new javax.swing.JButton();
        cmbMinutes = new javax.swing.JComboBox();
        lblminutes = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Reminder");

        pnReminder.setMaximumSize(new java.awt.Dimension(100, 120));
        pnReminder.setPreferredSize(new java.awt.Dimension(100, 100));

        lblReminder.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblReminder.setForeground(new java.awt.Color(0, 0, 102));
        lblReminder.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblReminder.setText("<html>This is example of your pop up reminder task...prevent scroll barsThis is example of your pop up reminder task...prevent scroll barsThis is example of your pop up reminder task...prevent scroll barsThis is example of your pop up reminder task...prev");
        lblReminder.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblReminder.setMaximumSize(new java.awt.Dimension(2147483647, 37));
        lblReminder.setPreferredSize(new java.awt.Dimension(1356, 37));

        javax.swing.GroupLayout pnReminderLayout = new javax.swing.GroupLayout(pnReminder);
        pnReminder.setLayout(pnReminderLayout);
        pnReminderLayout.setHorizontalGroup(
            pnReminderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnReminderLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblReminder, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnReminderLayout.setVerticalGroup(
            pnReminderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnReminderLayout.createSequentialGroup()
                .addComponent(lblReminder, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        getContentPane().add(pnReminder, java.awt.BorderLayout.CENTER);

        btnDismiss.setText("Dismiss");
        btnDismiss.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDismissActionPerformed(evt);
            }
        });

        btnSnooze.setText("Snooze");
        btnSnooze.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSnoozeActionPerformed(evt);
            }
        });

        cmbMinutes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "5", "10", "15", "20", "25", "30" }));

        lblminutes.setText("Minutes");

        javax.swing.GroupLayout pnButtonLayout = new javax.swing.GroupLayout(pnButton);
        pnButton.setLayout(pnButtonLayout);
        pnButtonLayout.setHorizontalGroup(
            pnButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnButtonLayout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(btnDismiss)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnSnooze)
                .addGap(6, 6, 6)
                .addComponent(cmbMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblminutes)
                .addGap(77, 77, 77))
        );
        pnButtonLayout.setVerticalGroup(
            pnButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(btnDismiss)
                .addComponent(btnSnooze)
                .addComponent(cmbMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblminutes))
        );

        pnButtonLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnSnooze, cmbMinutes, lblminutes});

        getContentPane().add(pnButton, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDismissActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDismissActionPerformed
        ReminderManager rm = new ReminderManager();
        reminder.setStatus("Done");
        rm.addOrUpdateReminder(reminder);
        this.dispose();
    }//GEN-LAST:event_btnDismissActionPerformed

    private void btnSnoozeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSnoozeActionPerformed
        ReminderManager rm = new ReminderManager();
        Calendar cal = Calendar.getInstance();
        // if there is no snooze time currently, means, minute to extend will be start from reminder time
        if (reminder.getSnoozeTime() == null)   cal.setTime(reminder.getTime());
        // else, the time extend to alert will be start from snooze time
        else cal.setTime(reminder.getSnoozeTime());
        cal.add(Calendar.MINUTE, Integer.parseInt((String) cmbMinutes.getSelectedItem()));
        reminder.setSnoozeTime(cal.getTime());
        reminder.setStatus("Pending");
        rm.addOrUpdateReminder(reminder);
        this.dispose();
    }//GEN-LAST:event_btnSnoozeActionPerformed


    Action snoozeAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            btnSnoozeActionPerformed(e);
        }
    };

    Action dismissAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            btnDismissActionPerformed(e);
        }
    };

    @Override
    public void windowOpened(WindowEvent e) {}

    @Override
    public void windowClosing(WindowEvent e) {
        ReminderManager rm = new ReminderManager();
        reminder.setStatus("Done");
        rm.addOrUpdateReminder(reminder);
    }

    @Override
    public void windowClosed(WindowEvent e) {}

    @Override
    public void windowIconified(WindowEvent e) {}

    @Override
    public void windowDeiconified(WindowEvent e) {}

    @Override
    public void windowActivated(WindowEvent e) {}

    @Override
    public void windowDeactivated(WindowEvent e) {}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDismiss;
    private javax.swing.JButton btnSnooze;
    private javax.swing.JComboBox cmbMinutes;
    private javax.swing.JLabel lblReminder;
    private javax.swing.JLabel lblminutes;
    private javax.swing.JPanel pnButton;
    private javax.swing.JPanel pnReminder;
    // End of variables declaration//GEN-END:variables
}
